//
//  BubbleChatView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/5/2.
//

import SwiftUI
import Kingfisher

struct BubbleChatView: View {
    @State var str: String
    @State var bean: BubbleBean
    @State var iconCenterLeading : CGFloat = 0
    @State var iconCenterTrailing : CGFloat = 0
    @State var iconCenterTop : CGFloat = 0
    @State var iconCenterBottom : CGFloat = 0
    
    var body: some View {
        ZStack {
            Text(str)
                .font(.system(size: 14))
                .foregroundColor(Color.hex(bean.textColor))
        }
        .padding(.leading, bean.leading)
        .padding(.trailing, bean.trailing)
        .padding(.top, bean.top)
        .padding(.bottom, bean.bottom)
        .frame(minWidth: bean.minWith, minHeight: bean.minHeight)
        .padding(.leading , iconCenterLeading)
        .padding(.trailing, iconCenterTrailing)
        .padding(.top , iconCenterTop)
        .padding(.bottom , iconCenterBottom)
        .background(
            BubbleBgView(bubble: bean)
        ).onAppear {
            iconCenterLeading = bean.iconCenter?.leading ?? 0
            iconCenterTrailing = bean.iconCenter?.trailing ?? 0
            iconCenterTop = bean.iconCenter?.top ?? 0
            iconCenterBottom = bean.iconCenter?.bottom ?? 0
        }
    }
    
    // 角标视图构建函数
    private func cornerImage(name: ImageResource, offset: CGSize) -> some View {
        Image(name)
            .offset(offset)
    }
}

private let previewBean: BubbleBean = {
        BubbleExt.getBubble(id: "5") ?? BubbleBean()
}()

#Preview {
    BubbleChatView(str: "哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽",bean: previewBean).background(.bg)
}
